package com.ddx.common.util;

import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * 一些通用的基本的工具方法
 */
public class CommonUtils {

    /**
     * 将空字符串转换成null
     */
    public static String convertBlankToNull(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return value;
    }

    /**
     * 将空数组转换成null
     */
    public static int[] convertBlankToNull(int[] values) {
        if (values != null && values.length ==0) {
            return null;
        }
        return values;
    }

    /**
     * 将空数组转换成null
     */
    public static String[] convertBlankToNull(String[] values) {
        if (values != null && values.length ==0) {
            return null;
        }
        return values;
    }

    /**
     * 用默认值替换null
     */
    public static <T> T ifNull(T value, T defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        return value;
    }



    /**
     * 切分一个大list成许多个小list
     */
    public static <T> List<List<T>> splitList(List<T> largeList, int maxItemCount) {
        List<List<T>> subLists = new ArrayList<>();
        if (largeList == null) {
            return subLists;
        }

        // 计算子列表的数量
        int listCount = largeList.size() / maxItemCount;
        if (largeList.size() % maxItemCount > 0) {
            ++listCount;
        }

        // 切割列表
        for (int i = 0; i < listCount; ++i) {
            int startIndex = i * maxItemCount;
            int endIndex = (i + 1) * maxItemCount;
            if (endIndex > largeList.size()) {
                endIndex = largeList.size();
            }

            List<T> subList = largeList.subList(startIndex, endIndex);
            subLists.add(subList);
        }

        return subLists;
    }

    /**
     * int(分) 转BigDecimal(元)
     */
    public static BigDecimal convertCentToYuan(int cent) {
        BigDecimal decimal = new BigDecimal(cent);
        decimal = decimal.divide(new BigDecimal(100));
        return decimal.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * BigDecimal(元)转 int(分)
     */
    public static int convertYuanToCent(BigDecimal yuan) {
        BigDecimal decimal = yuan.setScale(2, BigDecimal.ROUND_HALF_UP);
        return decimal.multiply(new BigDecimal(100)).intValue();
    }

    /**
     * int(分)转 String(元)
     */
    public static String convertCentToYuan2(int cent) {
      return convertCentToYuan(cent).toString();
    }

    /**
     * String(元)转 int(分)
     */
    public static int convertYuanToCent2(String yuan) {
      return convertYuanToCent(new BigDecimal(yuan));
    }

    public static String toIdString(int[] ids) {
        return toIdString(Ints.asList(ids));
    }

    public static String toIdString(long[] ids) {
        return toIdString(Longs.asList(ids));
    }

    public static String toIdString(List ids) {
        if (ids == null || ids.size() <= 0) {
            return "";
        }

        StringBuilder builder = new StringBuilder();
        builder.append(String.valueOf(ids.get(0)));
        for (int index = 1; index < ids.size(); ++index) {
            builder.append(",");
            builder.append(String.valueOf(ids.get(index)));
        }
        return builder.toString();
    }

    /**
     * 读取整个文件为字符串
     * @param path 文件路径
     * @return 文件内容
     */
    public static String readStringFromFile(String path) {
        try {
            return new String(Files.readAllBytes(Paths.get(path)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
